/*<!-- saved from url=(0013)about:internet -->*/
//*** Initializations

//Function checks if a string contains any special characters (string is not a word)
//Returns - 'true' if special char was found in String (not word), 'false' - otherwise
function notAword(str)
{
	return /[^a-zA-Z]/.test(str);
}


//isOK = true - Form is filled out correctly
//isOK = false - Form is filled out incorrectly
var isOK = true;


//Company name validation
function val_cname() 
{

	var name=document.getElementById("cname").value;

	if (name==null || name=="")
	{
		document.getElementById("companynameerror").style.visibility = "visible";
		isOK = false;
	}
	else if(notAword(name))
	{
		document.getElementById("companynameerror").style.visibility = "visible";
		isOK = false;
	}
	else
	{
		document.getElementById("companynameerror").style.visibility = "hidden";
	}

}

//Company Field validation
function val_cfield() 
{

	var field=document.getElementById("cfield").value;
	if (field==null || field=="")
	{
		document.getElementById("companyfielderror").style.visibility = "visible";
		isOK = false;
	}
	else if(notAword(field))
	{
		document.getElementById("companyfielderror").style.visibility = "visible";
		isOK = false;
	}
	else
	{
		document.getElementById("companyfielderror").style.visibility = "hidden";
	}


}


//E-mail validation
function val_email() 
{
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
	var email=document.getElementById("email").value;
	var res=emailPattern.test(email);
	if(res==false)
	{	
		document.getElementById("emailerror").style.visibility = "visible";
		isOK = false;	
	}
	
	else
	{
		document.getElementById("iderror").style.visibility = "hidden";
	}
	
}


//City validation
function val_city() 
{
	var city=document.getElementById("city").value;
	if (city==null || city=="")
	{
		document.getElementById("cityerror").style.visibility = "visible";
		isOK = false;
	}
	else if(notAword(city))
	{
		document.getElementById("cityerror").style.visibility = "visible";
		isOK = false;
	}
	else
	{
		document.getElementById("cityerror").style.visibility = "hidden";
	}
}



function val_zipcode() 
{

	//Zip code validation
	var zip=document.getElementById("zip").value;
	if (zip==null || zip=="")
	{
		document.getElementById("zipcodeerror").style.visibility = "visible";
		isOK = false;
	}
	else if(zip.length!=5)
	{
		document.getElementById("zipcodeerror").style.visibility = "visible";
		isOK = false;
	}

	else if(isNaN(zip))
	{
		document.getElementById("zipcodeerror").style.visibility = "visible";
		isOK = false;
	}
	
	else
	{
		document.getElementById("zipcodeerror").style.visibility = "hidden";
	}

}

//Checks if a string is not a word or number (contains special chars except 'space')
//returns true -  If special characters exist
//returns false - If only letters or digits exist
function notWordorNum(str) 
{	
	return /[^a-zA-Z0-9 ]/.test(str);
}

//Address validation
function val_add() 
{
	var address=document.getElementById("address").value;

	if(notWordorNum(address))
	{
		document.getElementById("addresserror").style.visibility = "visible";
		return false;
		
	}
	else
	{
		document.getElementById("addresserror").style.visibility = "hidden";
	}
	
}

function val_cell()
{
	//cell validation
	var cell=document.getElementById("cell").value;
	if (cell==null || cell=="")
	{
		alert("Cell number must be filled out");
		isOK = false;
	}
	if(cell.length!=10)
	{
		alert("Cell number must have 10 digits");
		isOK = false;
	}
	if(isNaN(cell))
	{
		alert("Cell number must contain digits only");
		isOK = false;
	}

}

function CheckAvailable()
{
        var uname = document.getElementById('uname').value;
        alert(uname);
        var xmlhttp;
		if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
        	//alert("The status is:");
        	if (xmlhttp.readyState==4&&xmlhttp.status==200)
            {
        	  		
        	  		//alert(xmlhttp.responseText);
        	  		if(xmlhttp.responseText == 'false')
                    {
                        document.getElementById('usernameerror').style.visibility= 'visible';                   
                    }
                    else
                    {
                        document.getElementById('usernameerror').style.visibility= 'hidden';
                    }
                }
          };
          xmlhttp.open("GET","http://localhost/WebProject/PHP/name_available.php?username="+uname,true);
          //xmlhttp.open("GET","sub.txt",true);
        xmlhttp.send();
}
function validateForm()
{
	val_fname();
	val_lname();
	val_id();
	val_email();
	val_city();
	val_zipcode();
	val_add();
	

var gender=document.getElementById("gender").value;
var mstatus=document.getElementById("mstatus").value;
var day=document.getElementById("day").value; 
var month=document.getElementById("month").value;
var year=document.getElementById("year").value;//should be YYYYMMDD




/*
//Username validation
var user=document.getElementById("uname").value;
if(user==null || user=="")
{
	alert("Username must be filled out");
	isOK = false;
	
}*/



//Password validation
var pass=document.getElementById("pass").value;
if (pass==null || pass=="")
{
	alert("Password must be filled out");
	isOK = false;
}

var passconf=document.getElementById("passconfirm").value;
if(passconf==null || passconf=="")
{
	alert("Password confirmation must be filled out");
	isOK = false;
}

if(pass!=passconf)
{
	alert("Password and Password confirm does not match");
	isOK = false;
}

if(isOK==false)
	return false;

}